﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="Suppliers.aspx.cs" Inherits="XYZ_Inventory.Suppliers" %>
<%@ Import Namespace="XYZ_Inventory" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <link href="Styles/GridView.css" rel="stylesheet" type="text/css"/>
    <link href="Styles/SearchBox.css" rel="stylesheet" type="text/css"/>
    <link href="Styles/DetailsView.css" rel="stylesheet" type="text/css"/>
    <link href="Styles/ValidationSummary.css" rel="stylesheet" type="text/css"/>
    <script src="Scripts/jquery-2.1.4.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.maskedinput.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $("[id^=ContentPlaceHolder1_GridView1_TextBox3]").mask("999-9999999");
        });

        jQuery(document).ready(function($) {
            $("#ContentPlaceHolder1_DetailsView1_tbContactIns").mask("999-9999999");
        });
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div style="padding: 0 1em 1em 1em;">
        <div class="searchBoxContainer">
            <asp:TextBox ID="txtSearch" CssClass="searchBox" runat="server" placeholder="Search by Name"></asp:TextBox>
            <asp:ImageButton ID="Button1" runat="server" AlternateText="Search" ImageUrl="~/Images/magnifying_glass_icon.png"/>
        </div>
        <div class="gridViewContainer">
            <asp:GridView ID="GridView1" CssClass="gridView" runat="server" AllowPaging="True"
                          AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID"
                          ForeColor="#333333" DataSourceID="SupplierSource" GridLines="None">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775"/>
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID"/>
                    <asp:TemplateField HeaderText="Name" SortExpression="Name">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" MaxLength="50" Text='<%# Bind("Name") %>'></asp:TextBox>
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox1" ErrorMessage="Supplier's name is required."
                                                        Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Edit">
                            </asp:RequiredFieldValidator>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Address" SortExpression="Address">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox2" Columns="30" runat="server" MaxLength="250" TextMode="MultiLine"
                                         Text='<%# Bind("Address") %>'>
                            </asp:TextBox>
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox2" ErrorMessage="Supplier's address is required."
                                                        Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Edit">
                            </asp:RequiredFieldValidator>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("Address").ToString().Replace(Environment.NewLine, "<br/>") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="District" SortExpression="District">
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlDistrict" runat="server" SelectedValue='<%# Bind("District") %>'
                                              DataSourceID="DistrictSource" DataTextField="DistrictName" DataValueField="Id"/>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <%# new XYZDBContext().Districts.AsEnumerable().Single(x => x.Id == (int) Eval("District")).DistrictName %>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Contact" SortExpression="Contact">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox3" runat="server" Width="90" MaxLength="11" Text='<%# Bind("Contact") %>'></asp:TextBox>
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox3" ErrorMessage="Supplier's contact number is required."
                                                        Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Edit">
                            </asp:RequiredFieldValidator>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("Contact") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Action" ShowHeader="False">
                        <EditItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" ValidationGroup="Edit"
                                            CommandName="Update" Text="Update">
                            </asp:LinkButton>
                            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                                                  Text="Cancel">
                            </asp:LinkButton>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                                            Text="Edit">
                            </asp:LinkButton>
                            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
                                                  Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');">
                            </asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EmptyDataRowStyle CssClass="emptyDataRow"></EmptyDataRowStyle>
                <EmptyDataTemplate>
                    <table class="gridView" style="border-collapse: collapse; color: #333333; width: 30em;">
                        <tr style="background-color: #5D7B9D; color: White; font-weight: bold;">
                            <th scope="col">
                                ID
                            </th>
                            <th scope="col">
                                Name
                            </th>
                            <th scope="col">
                                Address
                            </th>
                            <th scope="col">
                                Contact
                            </th>
                            <th scope="col">
                                Action
                            </th>
                        </tr>
                        <tr style="background-color: White; color: #284775;">
                            <td colspan="5">
                                NO SUPPLIER FOUND
                            </td>
                        </tr>
                    </table>
                </EmptyDataTemplate>
                <EditRowStyle BackColor="#999999"/>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"/>
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"/>
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center"/>
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333"/>
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"/>
                <SortedAscendingCellStyle BackColor="#E9E7E2"/>
                <SortedAscendingHeaderStyle BackColor="#506C8C"/>
                <SortedDescendingCellStyle BackColor="#FFFDF8"/>
                <SortedDescendingHeaderStyle BackColor="#6F8DAE"/>
            </asp:GridView>
            <asp:ValidationSummary ID="ValidationSummary2" Style="margin-top: 1em;" CssClass="validationSummary"
                                   HeaderText="<b>Please fix following error(s):</b>" ValidationGroup="Edit" ForeColor="Red"
                                   runat="server"/>
        </div>
        <br/>
        <asp:DetailsView ID="DetailsView1" CssClass="detailsView" runat="server" AutoGenerateRows="False"
                         CellPadding="4" DataKeyNames="ID" DataSourceID="SupplierSource" ForeColor="#333333"
                         GridLines="None" Height="50px" Width="125px" DefaultMode="Insert" OnItemInserted="DetailsView1_ItemInserted"
                         HeaderText="Register a supplier">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775"/>
            <CommandRowStyle BackColor="#E2DED6" Font-Bold="True"/>
            <EditRowStyle BackColor="#999999"/>
            <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True"/>
            <Fields>
                <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID"
                                InsertVisible="False"/>
                <asp:TemplateField HeaderText="Name" SortExpression="Name">
                    <InsertItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" MaxLength="50" Text='<%# Bind("Name") %>'></asp:TextBox>
                        <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox1" ErrorMessage="Supplier's name is required."
                                                    Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Insert">
                        </asp:RequiredFieldValidator>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Address" SortExpression="Address">
                    <InsertItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" MaxLength="250" TextMode="MultiLine" Text='<%# Bind("Address") %>'></asp:TextBox>
                        <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox2" ErrorMessage="Supplier's address is required."
                                                    Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Insert">
                        </asp:RequiredFieldValidator>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Contact" SortExpression="Contact">
                    <InsertItemTemplate>
                        <asp:TextBox ID="tbContactIns" runat="server" MaxLength="11" Width="90" Text='<%# Bind("Contact") %>'></asp:TextBox>
                        <asp:RequiredFieldValidator runat="server" ControlToValidate="tbContactIns" ErrorMessage="Supplier's contact number is required."
                                                    Text="*" ForeColor="Red" Display="Dynamic" ValidationGroup="Insert">
                        </asp:RequiredFieldValidator>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="District" SortExpression="District">
                    <InsertItemTemplate>
                        <asp:DropDownList ID="ddlDistrict" runat="server" SelectedValue='<%# Bind("District") %>'
                                          DataSourceID="DistrictSource" DataTextField="DistrictName" DataValueField="Id"/>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowInsertButton="True" ValidationGroup="Insert" InsertText="Register"
                                  ShowCancelButton="False"/>
                <asp:TemplateField ShowHeader="False">
                    <ItemStyle BackColor="#E2DED6"/>
                    <InsertItemTemplate>
                        <asp:ValidationSummary ID="ValidationSummary1" CssClass="validationSummary" HeaderText="<b>Please fix following error(s):</b>"
                                               ValidationGroup="Insert" ForeColor="Red" runat="server"/>
                    </InsertItemTemplate>
                </asp:TemplateField>
            </Fields>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"/>
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"/>
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center"/>
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333"/>
        </asp:DetailsView>
        <br/>
        <fieldset style="padding: 0.5em; width: 35em;">
            <legend>Import data from CSV</legend>
            <asp:FileUpload ID="FileUpload1" accept=".csv" BackColor="#5D7B9D" ForeColor="white" style="-ms-border-radius: 0.5em; border-radius: 0.5em; outline: none; padding: 0.5em;" runat="server"/>
            <asp:Button ID="Button2" OnClick="Button2_OnClick" style="-ms-border-radius: 0.5em; border-radius: 0.5em; outline: none; padding: 0.5em;" runat="server" Text="Import"/>
            <asp:Label ID="lblImport" runat="server"></asp:Label>
        </fieldset>
        <asp:EntityDataSource ID="SupplierSource" runat="server" ConnectionString="name=XYZDBContext"
                              DefaultContainerName="XYZDBContext" EnableDelete="True" EnableFlattening="False"
                              EnableInsert="True" EnableUpdate="True" EntitySetName="Suppliers1" EntityTypeFilter="Supplier"
                              Where="it.Name LIKE '%'+@Name +'%' OR @Name is NULL" OrderBy="it.ID DESC">
            <WhereParameters>
                <asp:ControlParameter ControlID="txtSearch" DbType="String" Name="Name" PropertyName="Text"/>
            </WhereParameters>
        </asp:EntityDataSource>
        <asp:EntityDataSource ID="DistrictSource" runat="server"
                              ConnectionString="name=XYZDBContext" DefaultContainerName="XYZDBContext"
                              EnableFlattening="False" EntitySetName="Districts" EntityTypeFilter="Districts">
        </asp:EntityDataSource>
    </div>
</asp:Content>